﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CarLoanSystem.Entity;

namespace CarLoanSystem.DAL
{
    public class LitigationInfoDetailDAL : DBBase
    {
        public static void Insert(LitigationDetailItem item)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlCommand command = new SqlCommand(@"INSERT INTO [CAR_LOAN].[dbo].[LITIGATION_DETAIL]
                                                       ([LITIGATION_ID]
                                                       ,[LITIGATION_DETAIL]
                                                       ,[LITIGATION_DETAIL_DATE])
                                                 VALUES
                                                      ( @LITIGATION_ID
                                                       ,@LITIGATION_DETAIL
                                                       ,@LITIGATION_DETAIL_DATE)", DBBase.con);

                command.Parameters.AddWithValue("@LITIGATION_ID", item.LITIGATION_ID);
                command.Parameters.AddWithValue("@LITIGATION_DETAIL", item.LITIGATION_DETAIL);
                command.Parameters.AddWithValue("@LITIGATION_DETAIL_DATE", item.LITIGATION_DETAIL_DATE);
                int tmp = command.ExecuteNonQuery();
                DBBase.DisConncetDatabase();
            }
            catch (Exception ex)
            {
                DBBase.DisConncetDatabase();
                throw ex;
            }
        }

        public static LitigationDetailItem LoadLitigationDetail(int LITIGATION_DETAIL_ID)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT [LITIGATION_DETAIL_ID]
                                                          ,[LITIGATION_ID]
                                                          ,[LITIGATION_DETAIL]
                                                          ,[LITIGATION_DETAIL_DATE]
                                                      FROM [CAR_LOAN].[dbo].[LITIGATION_DETAIL]
                                                    where [LITIGATION_DETAIL_ID] = @LITIGATION_DETAIL_ID ", DBBase.con);
                command.Parameters.AddWithValue("@LITIGATION_DETAIL_ID", LITIGATION_DETAIL_ID);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    LitigationDetailItem item = new LitigationDetailItem();
                    item.LITIGATION_DETAIL_ID = Convert.ToInt32(dt.Rows[0]["LITIGATION_DETAIL_ID"].ToString());
                    item.LITIGATION_ID = Convert.ToInt32(dt.Rows[0]["LITIGATION_ID"].ToString());
                    item.LITIGATION_DETAIL = dt.Rows[0]["LITIGATION_DETAIL"].ToString();
                    item.LITIGATION_DETAIL_DATE = Convert.ToDateTime(dt.Rows[0]["LITIGATION_DETAIL_DATE"].ToString());
                    return item;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static DataTable LoadAllLitigationDetail(int LITIGATION_ID)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT [LITIGATION_DETAIL_ID]
                                                          ,[LITIGATION_ID]
                                                          ,[LITIGATION_DETAIL]
                                                          ,[LITIGATION_DETAIL_DATE]
                                                      FROM [CAR_LOAN].[dbo].[LITIGATION_DETAIL]
                                                    where [LITIGATION_ID] = @LITIGATION_ID ", DBBase.con);
                command.Parameters.AddWithValue("@LITIGATION_ID", LITIGATION_ID);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                    DBBase.DisConncetDatabase();
                    return dt;
                }
                else
                {
                    DBBase.DisConncetDatabase();
                    return null;
                }

            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}